#Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
import json
import os
import stat


def print_kernel_name(ini_file):
    with open(ini_file, 'r') as f:
        line = f.readline().strip()
        if '[' in line and ']' in line:
            print(line[1:-1])
            return
    print("[ERROR] Wrong ops info ini.")
    return


def update_ops_info_json(canndev_info_json, ascend_info_json, ops_name):
    with open(canndev_info_json, 'r') as f:
        canndev_info = json.load(f)
    if ops_name not in canndev_info:
        print("[ERROR] Run gen_opinfo_json_from_ini.sh fail. There is no ops info of ", ops_name)
        return
    with open(ascend_info_json, 'r') as f:
        ascend_info = json.load(f)
    ascend_info[ops_name] = canndev_info[ops_name]

    fd = os.open(ascend_info_json, os.O_WRONLY, stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
    with os.fdopen(fd, 'w') as f:
        json.dump(ascend_info, f, indent=4, ensure_ascii=False)
    print("[INFO] Updata ops info success.")
